/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.flashnews.service;

import java.util.Date;
import java.util.List;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.entity.Article;
import com.thinkgem.jeesite.modules.cms.entity.ArticleData;
import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.flashnews.entity.ExFlashNews;
import com.thinkgem.jeesite.modules.flashnews.dao.ExFlashNewsDao;

/**
 * 视屏新闻Service
 * @author 张朋
 * @version 2016-05-19
 */
@Service
@Transactional(readOnly = true)
public class ExFlashNewsService extends CrudService<ExFlashNewsDao, ExFlashNews> {

	public ExFlashNews get(String id) {
		return super.get(id);
	}
	
	public List<ExFlashNews> findList(ExFlashNews exFlashNews) {
		return super.findList(exFlashNews);
	}
	
	public Page<ExFlashNews> findPage(Page<ExFlashNews> page, ExFlashNews exFlashNews) {
		return super.findPage(page, exFlashNews);
	}
	
	@Transactional(readOnly = false)
	public void save(ExFlashNews exFlashNews) {
        if (exFlashNews.getContent()!=null){
            exFlashNews.setContent(StringEscapeUtils.unescapeHtml4(
                    exFlashNews.getContent()));
        }
        // 如果没有审核权限，则将当前内容改为待审核状态
        if (!UserUtils.getSubject().isPermitted("cms:article:audit")){
            exFlashNews.setDelFlag(Article.DEL_FLAG_AUDIT);
        }
        // 如果栏目不需要审核，则将该内容设为发布状态
//        if (article.getCategory()!=null&& StringUtils.isNotBlank(article.getCategory().getId())){
//            Category category = categoryDao.get(article.getCategory().getId());
//            if (!Global.YES.equals(category.getIsAudit())){
//                article.setDelFlag(Article.DEL_FLAG_NORMAL);
//            }
//        }
        exFlashNews.setUpdateBy(UserUtils.getUser());
        exFlashNews.setUpdateDate(new Date());
//        if (StringUtils.isNotBlank(article.getViewConfig())){
//            article.setViewConfig(StringEscapeUtils.unescapeHtml4(article.getViewConfig()));
//        }

        ArticleData articleData = new ArticleData();;
        if (StringUtils.isBlank(exFlashNews.getId())){
            exFlashNews.preInsert();
//            articleData = article.getArticleData();
//            articleData.setId(article.getId());
            dao.insert(exFlashNews);
//            articleDataDao.insert(articleData);
        }else{
            exFlashNews.preUpdate();
//            articleData = article.getArticleData();
//            articleData.setId(article.getId());
            dao.update(exFlashNews);
//            articleDataDao.update(article.getArticleData());
        }
//        super.save(exFlashNews);
	}
	
	@Transactional(readOnly = false)
	public void delete(ExFlashNews exFlashNews) {
		super.delete(exFlashNews);
	}

    @Transactional(readOnly = false)
    public void updateHitsAddOne(String id) {
        dao.updateHitsAddOne(id);
    }
	
}